cmake_minimum_required(VERSION 2.6)
project(pro)
enable_language(CUDA)
option(CUDA_USE_STATIC_CUDA_RUNTIME OFF)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_BUILD_TYPE Debug)
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/workspace)

# 如果你是不同显卡，请设置为显卡对应的号码参考这里：https://developer.nvidia.com/zh-cn/cuda-gpus#compute
#set(CUDA_GEN_CODE "-gencode=arch=compute_75,code=sm_75")

# 如果你的opencv找不到，可以自己指定目录
set(OpenCV_DIR   "E:/opencv4.9.0/opencv/build")

# set(CUDA_TOOLKIT_ROOT_DIR     "E:/NVIDIA GPU Computing Toolkit/CUDA")
set(CUDNN_DIR    "E:/NVIDIA GPU Computing Toolkit/CUDA/v12.2")
# set(TENSORRT_DIR "/opt/TensorRT-8.4.1.5")
# RT-DETR 必须指定高版本的 tensorRT
set(TENSORRT_DIR "E:/TensorRT-10.0.1.6")

# 因为protobuf，需要用特定版本，所以这里指定路径
set(PROTOBUF_DIR "E:/protobuf/google")

find_package(CUDA REQUIRED)
find_package(OpenCV)

include_directories(
    ${CMAKE_SOURCE_DIR}/src
    ${CMAKE_SOURCE_DIR}/src/application
    ${CMAKE_SOURCE_DIR}/src/tensorRT
    ${CMAKE_SOURCE_DIR}/src/tensorRT/common
    ${OpenCV_INCLUDE_DIRS}
    ${PROTOBUF_DIR}/include
    ${TENSORRT_DIR}/include
    ${CUDNN_DIR}/include
)

# 切记，protobuf的lib目录一定要比tensorRT目录前面，因为tensorRTlib下带有protobuf的so文件
# 这可能带来错误
link_directories(
    ${PROTOBUF_DIR}/lib/Debug
    ${TENSORRT_DIR}/lib
    ${CUDNN_DIR}/lib/x64
    ${OpenCV_DIR}/lib
)

set(CMAKE_CXX_FLAGS  "${CMAKE_CXX_FLAGS} -std=c++11 -Wall -O0 -Wfatal-errors -pthread -w -g")
set(CUDA_NVCC_FLAGS "${CUDA_NVCC_FLAGS} -std=c++11 -O0 -Xcompiler -fPIC -g -w ${CUDA_GEN_CODE}")
file(GLOB_RECURSE cpp_srcs ${CMAKE_SOURCE_DIR}/src/*.cpp)
file(GLOB_RECURSE cuda_srcs ${CMAKE_SOURCE_DIR}/src/*.cu)
add_library(plugin_list SHARED ${cuda_srcs})
set_property(TARGET plugin_list PROPERTY CUDA_ARCHITECTURES native)

target_link_libraries(plugin_list nvinfer_10 nvinfer_plugin_10)
target_link_libraries(plugin_list cuda cublas cudart cudnn)
target_link_libraries(plugin_list libprotobufd)
target_link_libraries(plugin_list ${OpenCV_LIBS})

target_include_directories(plugin_list INTERFACE ${OpenCV_INCLUDE_DIRS}   )
target_include_directories(plugin_list INTERFACE ${CMAKE_SOURCE_DIR}/src   )
target_include_directories(plugin_list INTERFACE ${CMAKE_SOURCE_DIR}/src/application  )
target_include_directories(plugin_list INTERFACE ${CMAKE_SOURCE_DIR}/src/tensorRT   )
target_include_directories(plugin_list INTERFACE ${CMAKE_SOURCE_DIR}/src/tensorRT/common  )
target_include_directories(plugin_list INTERFACE ${PROTOBUF_DIR}/include   )
target_include_directories(plugin_list INTERFACE ${TENSORRT_DIR}/include   )
target_include_directories(plugin_list INTERFACE ${CUDNN_DIR}/include   )


# target_link_libraries(plugin_list opencv_core opencv_imgproc opencv_videoio opencv_highgui opencv_imgcodecs)

add_executable(pro ${cpp_srcs})
set_property(TARGET pro PROPERTY CUDA_ARCHITECTURES native)
# 如果提示插件找不到，请使用dlopen(xxx.so, NOW)的方式手动加载可以解决插件找不到问题
target_link_libraries(pro nvinfer nvinfer_plugin)
target_link_libraries(pro cuda cublas cudart cudnn)
target_link_libraries(pro protobuf pthread plugin_list)
target_link_libraries(pro ${OpenCV_LIBS})
# target_link_libraries(pro opencv_core opencv_imgproc opencv_videoio opencv_highgui opencv_imgcodecs)

add_custom_target(
    yolo
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro yolo
)

add_custom_target(
    yolo_pose
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro yolo_pose
)

add_custom_target(
    yolo_cls
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro yolo_cls
)

add_custom_target(
    yolo_seg
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro yolo_seg
)

add_custom_target(
    yolo_obb
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro yolo_obb
)

add_custom_target(
    bytetrack
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro bytetrack
)

add_custom_target(
    rtdetr
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro rtdetr
)

add_custom_target(
    test_yolo_map
    DEPENDS pro
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/workspace
    COMMAND ./pro test_yolo_map
)